package com.itheima.server.controller;


import com.itheima.server.service.UserInfoService;
import com.itheima.server.service.UserService;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserInfoService userInfoService;


    /**
     *接口名称：首次登录填写用户资料
     *接口路径：POST/user/loginReginfo
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody  UserInfo userInfo,@RequestHeader("Authorization") String token){
        //1.解析token，取出token的id给
        Claims claims = JwtUtils.getClaims(token); //claims就是map集合，里面有id与mobile
        Integer id = (Integer) claims.get("id");
        //2.给userInfo 设置user的id
        userInfo.setId(Long.valueOf(id));
        //3. 交个userService
        userInfoService.save(userInfo);

        return ResponseEntity.ok(null);
    }



    /**
     *接口名称：首次登录---补充头像
     *接口路径：POST/user/loginReginfo/head
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity loginReginfo(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        //1.解析token，取出token的id给
        Claims claims = JwtUtils.getClaims(token); //claims就是map集合，里面有id与mobile
        Integer id = (Integer) claims.get("id");
        //2. 把图片与id交给service去处理
        userInfoService.updateHeader(headPhoto,Long.valueOf(id));
        return ResponseEntity.ok(null);
    }
}
